/*
    Project: OCMF (Operant Chamber Management Framework)
    Version: 1.0
    Author: Copyright (C) 2010, Samuel Delaney & Andrew Harvey
    Licence:
        This file is part of OCMF.

            OCMF is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.

            OCMF is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.

            You should have received a copy of the GNU General Public License
            along with OCMF.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef OCMFSERVER_H
#define OCMFSERVER_H

#include <QThread>
#include <QQueue>
#include <QtGlobal>

#include "DbClient.h"
#include "OcmfServerStatus.h"
#include "ParallelPort.h"

class MainWindow;

class OcmfServer : public QThread
{
private:
    enum OcmfServerTask{
        Get_Experiment,
        Add_Experiment_Result,
        Set_Status,
        Synchronize_Images,
        Get_Experiment_Images,
        Get_Screen_Template
    };

    MainWindow * ownerMainWindow;

    bool aborted;
    bool isBusy;

    OcmfServerTask currentTask;

    DbClient * dbClient;

    ParallelPort * parallelPort;

    Experiment * waitForExperimentReturnVar;
    QByteArray *AddExperimentResultParameter;
    OcmfServerStatus::Status setServerStatusVar;
    QStringList getExperimentImagesParameter;
    QString getScreenTemplateParameter;
    ScreenTemplate * getScreenTemplateReturnVar;

    //allows the server thread to be aborted
    void Abort();

    MainWindow * OwnerMainWindow();

    //blocks the calling thread until an experiment is returned from the database - This is a synchronous call
    Experiment * WaitForExperiment();

    //sends the experiment result to the database - this is an asynchronous call and may return before the task has completed
    void AddExperimentResult(QByteArray * experimentResultBytes);

    //sets the servers status in the database - overides any status set internally - this is an asynchronous call and may return before the task has completed
    void SetServerStatus(OcmfServerStatus::Status ocmfServerStatus);

    //blocks the calling thread until all images are retreived from the database - This is a synchronous call
    void SynchronizeImagesFolder();

    void GetExperimentImages(QStringList imageFileNames);

    //blocks the calling thread until a screen template is returned from the database
    ScreenTemplate * GetScreenTemplate(QString name);

public:
    qlonglong serverId;
    OcmfServer(MainWindow *);
    ~OcmfServer();
protected:
    //function called when start() is called in constructor
    void run();
};

#endif // OCMFSERVER_H
